<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/23
 * Time: 16:52
 */

namespace app\api\controller;

use app\api\model\Banner;
use app\api\model\Music;
use think\Controller;
use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Session;
use think\Request;

class Article extends Base
{
    /**
     * 获取新闻列表ss
     * @return string
     */
    public function getArticle()
    {
        //限制接口调用次数
        $mark = Cache::get("article_list");
        if (!empty($mark)) {
            $data = unserialize($mark);
            return json(["code" => 1 , "message" => "成功" , "data" => $data]);
        }
        $article = new \app\api\model\Article();
        $data = $article->where("status","<>",0)->select();
        foreach ($data as $key => $value) {
            $data[$key]["reading"] = Cache::HGET("view",$value["id"]);
            if ($data[$key]["reading"] == "") {
                $data[$key]["reading"] = 0;
            }
        }
        Cache::set("article_list",serialize($data));
        return json(["code" => 1 , "message" => "成功" , "data" => $data]);
    }

    /**
     * 获取轮播图
     * @return string
     */
    public function getBanner()
    {
        $data = unserialize(Cache::get("banner"));
        //限制一分钟只能查询5次
        $count = Cache::get("bannerView") ?? 0;
        $count = intval($count);
        $life = Cache::get("life");
        if ($count > 4 || $life == "live") {
            Cache::set("bannerView",0);
            Cache::set("life","live",60);
            return json(["code" => 404, "message" => "未查询到轮播新闻"]);
        }
        Cache::set("bannerView",++$count);
        //数据库查询
        if (empty($data)) {
            $banner = new Banner();
            $idArr = $banner->column("article_id");
            $article = new \app\api\model\Article();
            $data = $article->whereIn("id", $idArr)->select();
            Cache::set("banner",serialize($data));
        }
        if (empty($data)) {
            return json(["code" => 404, "message" => "未查询到轮播新闻"]);
        }
        foreach ($data as $key => $value) {
            $data[$key]["imgSrc"] = "/images/upload/" . substr($data[$key]["imgSrc"], strrpos($data[$key]["imgSrc"], '/') + 1);
        }
        return json(["code" => 1 , "message" => "成功" , "data" => $data]);
    }

    /**
     * 新闻详情
     * @param Request $request
     * @return string
     */
    public function getArticleInfo(Request $request)
    {
        $id = intval($request->param()["id"]);
        //赋值
        $article = new \app\api\model\Article();
        //Redis存储浏览次数
        $count = Cache::hget("view",$id) ?? 0;
        $count = intval($count);
        $data["reading"] = ++$count;
        Cache::hset("view",$id,$data["reading"]);
        $time = Cache::get("time");
        if (is_null($time)) {
            Cache::set("time",1,time()+7200);
            $article->save(["reading" => $data["reading"]],["id" => $id]);
        }
        return json(["code" => 1, "message" => "成功", "data" => $data]);
    }

    /**
     * 改变收藏次数
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function alterCollected(Request $request)
    {
        $data = $request->param();
        if (isset($data)) {
            if ($data["collection"] == "true") {
                \app\api\model\Article::where("id",intval($data["id"]))->setInc("collection");
            } elseif ($data["collection"] == "false") {
                \app\api\model\Article::where("id",intval($data["id"]))->setDec("collection");
            }
        }
        return json(["code" => 1 , "message" => "操作成功" , "data" => $data]);
    }

}

function get_time($value) {
    if ($value > 3600) {
        return ceil($value/3600) . "小时前";
    } else {
        return ceil($value/60) . "分钟前";
    }
}

function get_data($value) {
    $num =  ceil(intval($value)/(24*60*60));
    if ($num >= 3) {
        $num = 3;
    }
    return $num ."天前";
}